package com.mind.ridge.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author Mind
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
        log.error(ex.getMessage());
        String message = ex.getMessage();
        if (message.contains("Duplicate entry")){
            String[] info = message.split(" ");

            return R.error(info[2]+"已存在！");
        }
        return R.error("未知错误！");
    }

    @ExceptionHandler(OrderException.class)
    public R<String> exceptionHandler(OrderException ex){
        log.error(ex.getMessage());
        return R.error(ex.getMessage());
    }
}
